﻿using Iworld.Administrator.Manager;
using Iworld.Administrator.Model;
using Iworld.Author.Manager;
using Iworld.Basis.Help;
using Iworld.Basis.Manager;
using Iworld.Game.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Main.Impl
{
    public static class InitManager
    {
        /// <summary>
        /// 初始化和加载系统服务
        /// </summary>
        public static void Init()
        {
            ManagerService.Initialize(new List<Assembly>() { new RIOfAuthor().GetAssembly(), new RIOfGame().GetAssembly(), new RIOfAdministrator().GetAssembly() });
            using (ModelToDbContext db = new ModelToDbContext())
            {
                if (!db.Database.Exists())
                {
                    AdministratorGroup g = new AdministratorGroup()
                    {
                        Name = SuperAcountInfoPackage.GroupName,
                        CanCreateRechargeCard = true,
                        CanEditAdministrator = true,
                        CanEditAdministratorGroup = true,
                        CanEditGameRound = true,
                        CanEditGameSetting = true,
                        CanEditUser = true, 
                        CanViewGameChapter=true,
                        CanViewGameRound=true, 
                        CanReviewWithdrawals=true
                    };
                    db.AdministratorGroups.Add(g);
                    Iworld.Administrator.Model.Administrator administrator =
                         new Iworld.Administrator.Model.Administrator()
                         {
                             Account = SuperAcountInfoPackage.Account,
                             Password = EncryptHelper.EncryptByMd5(SuperAcountInfoPackage.Password),
                             Username = SuperAcountInfoPackage.Username,
                             Group = g
                         };
                    db.Administrators.Add(administrator);
                    db.SaveChanges();
                    new SettingOfGame(db)
                    {
                        EachChapterTimeSpan = 40,
                        EachRoundTimeSpan = 40,
                        LimitRedGolds = 10000,
                        EachChapterRoundCount = 100
                    }.Save(db);
                }

            }
            new TimelineManager(new ModelToDbContext(), () => { return new ModelToDbContext(); }).Initialize();
        }
    }
}
